﻿#region Copyright(C) 2012 NCS Pte. Ltd. All rights reserved.

// ==================================================================================================
// Copyright(C) 2012 NCS Pte. Ltd. All rights reserved.
//
// SYSTEM NAME	:   iConnect.Azure Application Framework
// COMPONENT ID :   NCS.IConnect.Azure.Diagnostics.AzureStorage.AzureStorageTableServiceContext
// COMPONENT DESC:  	
//
// CREATED DATE/BY:  08 June 2012 / Lu Ya Ming
//
// REVISION HISTORY:	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================

#endregion

using System.Data.Services.Client;
using System.Linq;

using Microsoft.WindowsAzure.StorageClient;

namespace NCS.IConnect.Cloud.Core.AzureStorage
{
    /// <summary>
    /// The Azure storage table service context.
    /// </summary>
    public class AzureStorageTableServiceContext : ITableServiceContext
    {
        private readonly TableServiceContext context;

        /// <summary>
        /// Initializes a new instance of the <see cref="AzureStorageTableServiceContext"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public AzureStorageTableServiceContext(TableServiceContext context)
        {
            this.context = context;
        }

        /// <summary>
        /// Gets or sets the retry policy.
        /// </summary>
        /// <value>
        /// The retry policy.
        /// </value>
        public RetryPolicy RetryPolicy
        {
            get { return this.context.RetryPolicy; }
            set { this.context.RetryPolicy = value; }
        }

        /// <summary>
        /// Creates the query.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entitySetName">Name of the entity set.</param>
        /// <returns></returns>
        public IQueryable<T> CreateQuery<T>(string entitySetName)
        {
            return this.context.CreateQuery<T>(entitySetName);
        }

        /// <summary>
        /// Adds the object.
        /// </summary>
        /// <param name="entitySetName">Name of the entity set.</param>
        /// <param name="entity">The entity.</param>
        public void AddObject(string entitySetName, object entity)
        {
            this.context.AddObject(entitySetName, entity);
        }

        /// <summary>
        /// Attaches to.
        /// </summary>
        /// <param name="entitySetName">Name of the entity set.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="etag">The etag.</param>
        public void AttachTo(string entitySetName, object entity, string etag)
        {
            this.context.AttachTo(entitySetName, entity, etag);
        }

        /// <summary>
        /// Updates the object.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void UpdateObject(object entity)
        {
            this.context.UpdateObject(entity);
        }

        /// <summary>
        /// Saves the changes with retries.
        /// </summary>
        /// <param name="options">The options.</param>
        public void SaveChangesWithRetries(SaveChangesOptions options)
        {
            this.context.SaveChangesWithRetries(options);
        }
    }
}
